package TicketsThread;

public class SystemStack {
    char[] ticket = new char[6];
    int cnt;
    int num = 1;

    public synchronized void push(char ch) {
        while (cnt == 6) {
            try {
                this.wait();
            } catch (InterruptedException e) {
//                e.printStackTrace();
            }
        }
//        System.out.println(cnt);

        this.notify();
        ticket[cnt] = ch;
        cnt++;
        System.out.println("生产了" + ch + "\n当前容器中有" + cnt + "张票\n");
    }

    public synchronized void pop() {

        while (cnt == 0) {
            try {
                this.wait();
            } catch (InterruptedException e) {
//                e.printStackTrace();
            }
        }

        this.notify();

        char temp = ticket[cnt - 1];

        cnt--;
        System.out.println("消费了" + temp + "\n当前容器中有" + cnt + "张票\n");


        System.out.println("计划了" + "[" + num + "]" + "\n");
        num++;

        if (num == 21) {
            System.exit(0);
        }
    }
}
